home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / dskut / crc113.zip / CRC112.C < prev    next >
Text File  |  1990-01-02  |  2KB  |  106 lines

  1. #include <stdio.h>
  2. #include <dir.h>
  3. #include <dos.h>
  4. #include "calc.h"
  5. #include "file.h"
  6. #include "write.h"
  7.  
  8. char prgm_id[] = "\nCRC-M2 - Cyclic Redundancy Checker"
  9.          ", Version 1.12, 01/02/90, rtk\n";
  10.  
  11. #define ONE_K     1024
  12. #define MAX_BUF   (32 * ONE_K - 1)
  13.  
  14. unsigned int buf_size;
  15. char *buf;
  16.  
  17. void disp_help(void)
  18. {
  19.   wrstr("\nUsage:  CRC filespec [filespec...]\n");
  20. }
  21.  
  22. void strins(char *dest, char *src, int i)
  23. {
  24.   int slen, dlen;
  25.  
  26.   slen = strlen(src);
  27.   dlen = strlen(dest);
  28.   memmove(dest+i+slen, dest+i, dlen-i+1);
  29.   memmove(dest+i, src, slen);
  30. }
  31.  
  32. process(char *path, char *name)
  33. {
  34.   char pn[MAXPATH];
  35.   int fd;
  36.   int bytes_read;
  37.   unsigned int crc16;
  38.   unsigned long crc32;
  39.  
  40.   strcpy(pn, path);
  41.   strcat(pn, name);
  42.   wrchar('\t');
  43.   wrstr(name);
  44.   wr_char_rep(' ', 12-strlen(name));
  45.   wrstr(": ");
  46.  
  47.   fd = file_open(pn);
  48.  
  49.   crc16 = 0;
  50.   crc32 = 0xffffffff;   /* pre-condition - bits all ones */
  51.  
  52.   do {
  53.     bytes_read = rdbin(fd, buf, buf_size);
  54.     update_crc(buf, bytes_read, &crc16, &crc32);
  55.   } while (bytes_read == buf_size);
  56.  
  57.   crc32 ^= 0xffffffff;  /* post-condition - one's complement */
  58.  
  59.   file_close(fd);
  60.  
  61.   wrstr("crc16=");
  62.   wrhex(crc16);
  63.   wrstr("  crc32=");
  64.   wrlnghex(crc32);
  65.   wrchar('\n');
  66. }
  67.  
  68. main(int argc, char *argv[])
  69. {
  70.   struct ffblk ffblk;
  71.   int i, attr = FA_HIDDEN + FA_SYSTEM;
  72.   char path[MAXPATH];
  73.  
  74.   wrstr(prgm_id);
  75.   if (argc == 1) {
  76.     disp_help();
  77.     return;
  78.   } else
  79.     wrchar('\n');
  80.  
  81.   make_table();
  82.   buf_size = coreleft() - ONE_K;
  83.   if (buf_size > MAX_BUF)
  84.     buf_size = MAX_BUF;
  85.   buf = (char *)malloc(buf_size);
  86.  
  87.   for (i = 1; i < argc; ++i) {
  88.     if (findfirst(argv[i], &ffblk, attr) == 0) {
  89.       if (i > 1)
  90.         wrchar('\n');
  91.       strcpy(path, argv[i]);
  92.       strupr(path);
  93.       get_path(path);
  94.       wrstr(path);
  95.       wrchar('\n');
  96.  
  97.       do {
  98.         process(path, ffblk.ff_name);
  99.       } while (findnext(&ffblk) == 0);
  100.     } else {
  101.       wrstr(argv[i]);
  102.       wrstr(": Not found\n");
  103.     }
  104.   }
  105. }
  106.